import { logError } from "./useLog.js";

export function loop(work, delay = 1000) {
  let timer = null;

  const run = async () => {
    try {
      await work();
    } catch (error) {
      logError(`Error: ${error.message}`);
    } finally {
      timer = null;
      timer = setTimeout(run, delay);
    }
  };

  // setTimeout(run, delay);
  run();

  return () => {
    clearTimeout(timer);
    timer = null;
  };
}
